package com.github.postsanf.yinian.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.utils.StringUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONObject;

import java.util.LinkedHashMap;

public class ShopContactServiceActivity extends BaseActivity implements View.OnClickListener {
    private EditText shopcontact_detail;
    private TextView shopcontact_name;
    private TextView shopcontact_weixin;
    private TextView shopcontact_email;
    private TextView shopcontact_telpheone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopcontactservice);
        initView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shopcontact_back:
                finish();
                break;
            case R.id.shopcontact_submit:
                doSubmit();
                break;
        }
    }

    private void initView() {
        findViewById(R.id.shopcontact_back).setOnClickListener(this);
        findViewById(R.id.shopcontact_submit).setOnClickListener(this);
        shopcontact_detail = (EditText) findViewById(R.id.shopcontact_detail);
        shopcontact_name = (TextView) findViewById(R.id.shopcontact_name);
        shopcontact_weixin = (TextView) findViewById(R.id.shopcontact_weixin);
        shopcontact_email = (TextView) findViewById(R.id.shopcontact_email);
        shopcontact_telpheone = (TextView) findViewById(R.id.shopcontact_telpheone);

    }

    private void doSubmit() {
        if (StringUtils.isEmpty(shopcontact_detail.getText().toString())) {
            Toast.makeText(ShopContactServiceActivity.this, "内容不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(shopcontact_name.getText().toString())) {
            Toast.makeText(ShopContactServiceActivity.this, "收货人不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(shopcontact_weixin.getText().toString())) {
            Toast.makeText(ShopContactServiceActivity.this, "微信不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(shopcontact_email.getText().toString())) {
            Toast.makeText(ShopContactServiceActivity.this, "邮箱不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (StringUtils.isEmpty(shopcontact_telpheone.getText().toString())) {
            Toast.makeText(ShopContactServiceActivity.this, "电话号码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        ContactCustomerService(shopcontact_detail.getText().toString(),
                shopcontact_name.getText().toString(),
                shopcontact_weixin.getText().toString(),
                shopcontact_email.getText().toString(),
                shopcontact_telpheone.getText().toString());
    }

    private void ContactCustomerService(String content, String name,
                                        String wechatID,
                                        String email,
                                        String phonenumber) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        mParam.put("content", content);
        mParam.put("name", name);
        mParam.put("wechatID", wechatID);
        mParam.put("email", email);
        mParam.put("phonenumber", phonenumber);
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynContactCustomerService))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s.toString());
                            if (jsonObject.optString("msg").equals("success") && jsonObject.optInt("code") == 0) {
                                showToast("提交成功");
                                finish();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }


    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
